<?php


namespace App\Http\Controllers\Admin;

use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

//银行卡
class BankCardController extends BaseController
{
    /**
     * @Desc:列表
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function list(Request $request)
    {
        $param = $request->only(['page', 'page_size', 'user_code', 'cardholder', 'card_number', 'mobile']);
        $v = app('validator')->make($param, [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1',
            'user_code' => 'filled|integer',
            'cardholder' => 'filled|string',
            'card_number' => 'filled|string',
            'mobile' => 'filled|string',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_bank_card')->list($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:详情
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function info(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_bank,id',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_bank_card')->info($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:新增
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function create(Request $request)
    {
        $param = $request->only([
            'user_code',
            'bank_num',
            'cardholder',
            'bank_name',
            'mobile',
            'card_number',
            'bank_zhi',
        ]);
        $v = app('validator')->make($param, [
            'user_code' => 'required|integer|min:0',
            'bank_num' => 'required|string|min:16',
            'bank_name' => 'required|string|max:255',
            'mobile' => 'required|string|max:11|regex:/^1[3456789]\d{9}$/',
            'card_number' => 'required|string|max:18',
            'bank_zhi' => 'required|string|max:255',
            'cardholder' => 'required|string|min:2|max:50',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_bank_card')->create($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:更新
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function update(Request $request)
    {
        $param = $request->only([
            'id',
            'user_code',
            'bank_num',
            'cardholder',
            'bank_name',
            'mobile',
            'card_number',
            'bank_zhi',
        ]);
        $v = app('validator')->make($param, [
            'id' => 'required|integer|exists:mysql.ln_user_bank,id',
            'user_code' => 'required|integer|min:0',
            'bank_num' => 'required|string|min:16',
            'bank_name' => 'required|string|max:255',
            'mobile' => 'required|string|max:11|regex:/^1[3456789]\d{9}$/',
            'card_number' => 'required|string|max:18',
            'bank_zhi' => 'required|string|max:255',
            'cardholder' => 'required|string|min:2|max:50',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $list = app('repo_bank_card')->update($param);
        return Response::sendData($list);
    }

    /**
     * @Desc:删除
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/9/28 23:39
     */
    public function delete(Request $request)
    {
        $param = $request->only(['ids']);
        $v = app('validator')->make($param, [
            'ids' => 'required|array',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_bank_card')->delete($param);
        return Response::sendData(true);
    }
}